import * as Cesium from 'cesium'
import DrawBillboard from './DrawBillboard'
import DrawPolyline from './DrawPolyline'
import DrawPolygon from './DrawPolygon'
export class DrawPolt {
  constructor (arg) {
    this.viewer = arg.viewer
    this._billboardArr = []
    this._lineArr = []
    this._gonArr = []
    this.handler = null
    this.canvasElement = null
  }

  get billboardArr () {
    return this._billboardArr
  }

  setCanvas (element) {
    if (!this.canvasElement) { this.canvasElement = element }
  }

  get polylineArr () {
    return this._lineArr
  }

  get polygonArr () {
    return this._gonArr
  }

  create (type) {
    this.canvasElement.style.cursor = 'crosshair'
    if (!type) {
      console.warn('请选择类型！')
    }
    if (type === 'drawpoint') { // 绘制billboard
      var billboard = new DrawBillboard({
        viewer: this.viewer,
        canvasElement: this.canvasElement
      })
      billboard.startCreate()
      this._billboardArr.push(billboard)
    } else if (type === 'drawpolyline') { // 绘制线
      var line = new DrawPolyline({
        viewer: this.viewer,
        canvasElement: this.canvasElement
      })
      line.startCreate()
      this._lineArr.push(line)
    } else { // 绘制面
      var gon = new DrawPolygon({
        viewer: this.viewer,
        canvasElement: this.canvasElement
      })
      gon.startCreate()
      this._gonArr.push(gon)
    }
  }

  clearAll () {
    this.viewer.entities.removeAll()
    this._billboardArr = []
    this._lineArr = []
    this._gonArr = []
    /*   for (var i = 0; i < this._lineArr.length; i++) {
        var line = this._lineArr[i]
        line.destroy()
      }
      this._lineArr = []
      for (var j = 0; j < this._gonArr.length; j++) {
        var gon = this._gonArr[j]
        gon.destroy()
      }
      this._gonArr = []
      for (var k = 0; k < this._billboardArr.length; k++) {
        var billboard = this._billboardArr[k]
        billboard.destroy()
      }
      this._billboardArr = [] */
  }

  clearOne () {
    var $this = this
    this.canvasElement.style.cursor = 'crosshair'
    if (!this.handler) {
      this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas)
    }
    var that = this
    this.handler.setInputAction(function (evt) {
      var pick = that.viewer.scene.pick(evt.position)
      if (Cesium.defined(pick) && pick.id) {
        that.viewer.entities.remove(pick.id)
        that.handler.destroy()
        that.handler = null
        $this.canvasElement.style.cursor = 'auto'
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK)
  }
}
